import {
  Controller,
  Get,
  Post,
  // Patch,
  // Param,
  // Delete,
  HttpCode,
  Request,
  UseGuards,
  Body,
} from '@nestjs/common';
import { ApiOperation } from '@nestjs/swagger';
import { AuthService } from './auth.service';
import { RefreshJwtGuard } from './guards/refresh-jwt.guard';
import { CreateAuthDto } from './dto/create-auth.dto';
import { User } from '../user/entities/user.entity';
import { JWT_User } from './dto/update-auth.dto';
import { Public,Permissions } from '../public/public.decorator';
import { LogOperationTitle } from 'src/common/decorator/oprertionlog.decorator';

@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Post('login')
  @Public()
  @ApiOperation({ summary: '用户登录' })
  @LogOperationTitle('用户登录')
  async login(@Body() body: CreateAuthDto) {
    const user = await this.authService.login(body);
    return user;
    // 验证用户凭据...
    // const tokens = await this.authService.generateTokens(req.user);

    // return {
    //   accessToken: tokens.accessToken,
    //   refreshToken: tokens.refreshToken, // 返回refresh_token到响应体
    //   user: req.user,
    // };
  }

  @UseGuards(RefreshJwtGuard)
  @Post('refreshToken')
  @Public()
  @ApiOperation({ summary: '刷新token' })
  @LogOperationTitle('刷新token')
  @HttpCode(200)
  async refresh(@Request() req: { user: User }) {
    const tokens = await this.authService.refreshTokens(req.user);

    return tokens;
  }

  @Get('getUserInfo')
  @ApiOperation({ summary: '获取用户信息' })
  @LogOperationTitle('获取用户信息')
  getUserInfo(@Request() req: { user: JWT_User }) {
    return this.authService.getUserInfo(req.user);
  }
}
